using Core.Framework.Model.Common;
using Core.Middleware.ApiJson;
using Core.Middleware.ApiRelationUrl;
using Core.Middleware.DynamicApi;
using Core.Middleware.StaticFileCacheing;
using Core.Middleware.WebSockets;
using Core.Middleware.XMLDiscription;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

namespace Core.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            CoreStartupHelper.Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            var origins = new[] { "http://aaa.itool.store/" };

            services
                .AddCors(options =>
                            options.AddPolicy("_default",
                         p =>
                            p
                            .WithOrigins(origins)
                            .AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowCredentials()
                           ));
            BuildServiceProvider.DIRegister(services);
            services.AddSwaggerService();
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
        {
            // 系统生命周期触发事件
            lifetime.RegisterApplicationLifetime(app);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // 跨域
            app.UseCors("_default");

            // 启用Https
            //app.UseHttpsRedirection();

            // 启用静态资源
            app.UseStaticFiles();

            // 启用 XMLDiscription
            app.XMLDiscriptionMidWare();

            // 启用静态文件缓存
            app.UseStaticFileCacheMidWare();

            // WS中间件
            app
                .UseWebSocketsMiddleware()
                .UseQueueLoop()
                .UseQueueMsgBackups();

            // 注册 Swagger-Doc
            app.RegisterSwagger();

            // 启用动态API中间件
            app.UseDynamicApiMidWare();

            // 在上下文验证项目信息
            app.UseApiRelationUrlMidWare();

            // 启用 Api Json
            app.UseApiJsonMidWare();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
